/*
 *  Openmysee
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "stdafx.h"
#include ".\mediapintype.h"

SampleBuffer::SampleBuffer(void):m_buffer(NULL)
{
}

SampleBuffer::~SampleBuffer()
{
    delete[] m_buffer;
}

bool    SampleBuffer::IsAnyDataSaved()
{
    return m_buffer != NULL;
}

void    SampleBuffer::SaveData(const SampleHeader& header, const BYTE* pData, const UINT maxSize)
{
    if(m_buffer != NULL)
        Clean();

    if(header.size == 0)
    {
        m_size = maxSize;
    }
    else
        m_size = header.size;
    
    m_buffer = new BYTE[sizeof(SampleHeader) + m_size];
    memcpy(m_buffer, &header, sizeof(SampleHeader));
    memcpy(m_buffer + sizeof(SampleHeader), pData, m_size);
}

bool    SampleBuffer::PeerData(SampleHeader& header, BYTE* pData, const UINT maxSize) const
{
    if(m_buffer == NULL)
        return false;

    memcpy(&header, m_buffer, sizeof(SampleHeader));

    UINT datalen;
    if(m_size > maxSize)
    {
        datalen = maxSize;
    }
    else
        datalen = m_size;

    memcpy(pData, m_buffer + sizeof(SampleHeader), datalen);
    return true;
}

bool    SampleBuffer::GetData(SampleHeader& header, BYTE* pData, const UINT maxSize)
{
    if(!PeerData(header, pData, maxSize))
        return false;

    Clean();
    return true;

}

bool    SampleBuffer::Alloc(SampleHeader* &pHeaderbuf, BYTE* &pDatabuf, const size)
{
    Clean();
    m_size = size;
    m_buffer = new BYTE[sizeof(SampleHeader) + m_size];
    pHeaderbuf = reinterpret_cast<SampleHeader*>(m_buffer);
    pDatabuf = m_buffer + sizeof(SampleHeader);

    return true;
}

void    SampleBuffer::Clean()
{
    delete[] m_buffer;
    m_buffer = NULL;
}


MediaPinType::MediaPinType(void)
{
}

MediaPinType::~MediaPinType(void)
{
}

void MediaPinType::SaveMediaTypeData(const SampleHeader& header, const BYTE* pData, const UINT maxSize)
{
    UINT len;
    if(maxSize < 256)
        len = maxSize;
    else
        len = 256;

    SaveData(header, pData, len);
}

bool MediaPinType::RestoreMediaTypeData(SampleHeader& header, BYTE* pData, const UINT maxSize) const
{
    UINT len;
    if(maxSize < 256)
        len = maxSize;
    else
        len = 256;

    return PeerData(header, pData, len);
}

